package com.leedsoft.service;

import com.leedsoft.common.DateUtil;
import com.leedsoft.common.Page;
import com.leedsoft.controller.dto.AidMemDto;
import com.leedsoft.controller.dto.OrgMemDto;
import com.leedsoft.controller.vo.NewOrgMemVo;
import com.leedsoft.controller.vo.OrgMemPageVo;
import com.leedsoft.domain.*;
import com.leedsoft.mapper.AidMemMapper;
import com.leedsoft.mapper.AidOrgMapper;
import com.leedsoft.mapper.AidOrgMemMapper;
import com.leedsoft.mapper.SysPmiUserMapper;
import com.leedsoft.mapper.self.OrgMemSelfMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrgMemServiceImpl implements OrgMemService {

    @Autowired
    private SysPmiUserMapper sysPmiUserMapper;

    @Autowired
    private AidOrgMapper aidOrgMapper;

    @Autowired
    private AidMemMapper aidMemMapper;

    @Autowired
    private AidOrgMemMapper aidOrgMemMapper;

    @Autowired
    private OrgMemSelfMapper orgMemSelfMapper;

    @Override
    public Page<OrgMemDto> getOrgMemPage(OrgMemPageVo memPageVo) {
        Page<OrgMemDto> page = new Page<>(memPageVo);
        int count = orgMemSelfMapper.getOrgMemListCount(memPageVo);
        page.setCount(count);
        if (count!=0){
            List<OrgMemDto> list = orgMemSelfMapper.getOrgMemList(memPageVo);
            page.setList(list);
        }
        return page;
    }

    @Override
    public List<SysPmiUser> querySysUser() {
        return sysPmiUserMapper.selectByExample(new SysPmiUserExample());
    }

    @Override
    public List<AidOrg> queryAidOrg() {
        return aidOrgMapper.selectByExample(new AidOrgExample());
    }

    @Override
    @Transactional
    public void addOrgMem(NewOrgMemVo newOrgMemVo) {
        //分别向两张表插入数据  aid_mem   aid_org_mem
        AidMem aidMem = new AidMem();
        AidMemExample memExample = new AidMemExample();
        if (newOrgMemVo.getUser_id()!=null){//user_id 不等于空，表示添加的用户属于系统用户
            memExample.createCriteria().andUser_idEqualTo(newOrgMemVo.getUser_id());
            long count = aidMemMapper.countByExample(memExample);//检查该系统用户是否被添加
            System.out.println("是否被添加："+count);
            if (count==0){//未被添加
                aidMem.setContact(newOrgMemVo.getContact());
                aidMem.setName(newOrgMemVo.getName());
                if (newOrgMemVo.getIs_sys_user()==null){
                    aidMem.setIs_sys_user(AidMemDto.IS_SYS_USER);
                }else {
                    aidMem.setIs_sys_user(newOrgMemVo.getIs_sys_user());
                }
                aidMem.setUser_id(newOrgMemVo.getUser_id());
                aidMem.setCreate_time(DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
                aidMemMapper.insert(aidMem);
            }else {//已经添加过该系统用户了
                memExample = new AidMemExample();
                memExample.createCriteria().andUser_idEqualTo(newOrgMemVo.getUser_id());
                List<AidMem> list = aidMemMapper.selectByExample(memExample);
                aidMem = list.get(0);//取出该系统用户，获取id,用于下面的赋值
            }
        }else {//user_id 为空，表示添加的用户不属于系统用户
            aidMem.setContact(newOrgMemVo.getContact());
            aidMem.setName(newOrgMemVo.getName());
            if (newOrgMemVo.getIs_sys_user()==null){
                aidMem.setIs_sys_user(AidMemDto.NO_SYS_USER);
            }else {
                aidMem.setIs_sys_user(newOrgMemVo.getIs_sys_user());
            }
            aidMem.setIs_sys_user(newOrgMemVo.getIs_sys_user());
            aidMem.setCreate_time(DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
            aidMemMapper.insert(aidMem);
        }
        AidOrgMem aidOrgMem = new AidOrgMem();
        aidOrgMem.setMem_id(aidMem.getId());
        aidOrgMem.setOrg_id(newOrgMemVo.getOrg_id());
        aidOrgMem.setTitle(newOrgMemVo.getTitle());
        aidOrgMem.setCreate_time(DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        aidOrgMemMapper.insert(aidOrgMem);
    }

    @Override
    @Transactional
    public void updateOrgMem(NewOrgMemVo newOrgMemVo) {
        AidMem aidMem = new AidMem();
        aidMem.setId(newOrgMemVo.getMem_id());
        aidMem.setContact(newOrgMemVo.getContact());
        aidMem.setName(newOrgMemVo.getName());
        aidMem.setUpdate_time(DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        aidMemMapper.updateByPrimaryKeySelective(aidMem);

        AidOrgMem aidOrgMem = new AidOrgMem();
        aidOrgMem.setId(newOrgMemVo.getOm_id());
        aidOrgMem.setMem_id(newOrgMemVo.getMem_id());
        aidOrgMem.setOrg_id(newOrgMemVo.getOrg_id());
        aidOrgMem.setTitle(newOrgMemVo.getTitle());
        aidOrgMem.setUpdate_time(DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        aidOrgMemMapper.updateByPrimaryKey(aidOrgMem);
    }

    @Override
    @Transactional
    public void deleteOrgMem(long id,long mem_id) {
        aidOrgMemMapper.deleteByPrimaryKey(id);
        AidOrgMemExample aidOrgMemExample = new AidOrgMemExample();
        aidOrgMemExample.createCriteria().andMem_idEqualTo(mem_id);
        long count = aidOrgMemMapper.countByExample(aidOrgMemExample);
        if (count<1){
            aidMemMapper.deleteByPrimaryKey(mem_id);
        }
    }

}
